package controller

import (
	"net/http"
	"strconv"

	"gitee.com/wangshh_123/cross-pay-go/src/models"
	"gitee.com/wangshh_123/cross-pay-go/src/service"
	ucommon "gitee.com/wangshh_123/cross-pay-go/src/utils/common"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// UserCardFeeConfigController 用户卡费配置控制器
type UserCardFeeConfigController struct {
	db *gorm.DB
}

// NewUserCardFeeConfigController 创建用户卡费配置控制器实例
func NewUserCardFeeConfigController(db *gorm.DB) *UserCardFeeConfigController {
	return &UserCardFeeConfigController{
		db: db,
	}
}

// CreateUserCardFeeConfig 创建用户卡费配置绑定
func (c *UserCardFeeConfigController) CreateUserCardFeeConfig(ctx *gin.Context) {
	var req models.UserCardFeeConfigCreateRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_PARAMS", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 创建绑定
	config, err := service.CreateUserCardFeeConfig(&req)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "CREATE_USER_CARD_FEE_CONFIG_FAILED", err)
		return
	}

	ucommon.Success(ctx, config)
}

// UpdateUserCardFeeConfig 更新用户卡费配置状态
func (c *UserCardFeeConfigController) UpdateUserCardFeeConfig(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_ID", err)
		return
	}

	var req models.UserCardFeeConfigUpdateRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_PARAMS", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 更新配置
	config, err := service.UpdateUserCardFeeConfig(id, &req)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "UPDATE_USER_CARD_FEE_CONFIG_FAILED", err)
		return
	}

	ucommon.Success(ctx, config)
}

// GetUserCardFeeConfig 获取用户卡费配置
func (c *UserCardFeeConfigController) GetUserCardFeeConfig(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_ID", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 获取配置
	config, err := service.GetUserCardFeeConfig(id)
	if err != nil {
		ucommon.Error(ctx, http.StatusNotFound, "USER_CARD_FEE_CONFIG_NOT_FOUND", err)
		return
	}

	ucommon.Success(ctx, config)
}

// GetUserCardFeeConfigs 获取用户卡费配置列表
func (c *UserCardFeeConfigController) GetUserCardFeeConfigs(ctx *gin.Context) {
	// 解析查询参数
	query := &models.UserCardFeeConfigQuery{
		Page:     1,
		PageSize: 10,
	}

	// 用户ID
	if userIDStr := ctx.Query("user_id"); userIDStr != "" {
		if userID, err := strconv.ParseInt(userIDStr, 10, 64); err == nil {
			query.UserID = &userID
		}
	}

	// 卡费配置ID
	if configIDStr := ctx.Query("card_fee_config_id"); configIDStr != "" {
		if configID, err := strconv.ParseInt(configIDStr, 10, 64); err == nil {
			query.CardFeeConfigID = &configID
		}
	}

	// 状态
	if status := ctx.Query("status"); status != "" {
		query.Status = &status
	}

	// 分页参数
	if pageStr := ctx.Query("page"); pageStr != "" {
		if page, err := strconv.Atoi(pageStr); err == nil && page > 0 {
			query.Page = page
		}
	}
	if pageSizeStr := ctx.Query("page_size"); pageSizeStr != "" {
		if pageSize, err := strconv.Atoi(pageSizeStr); err == nil && pageSize > 0 && pageSize <= 100 {
			query.PageSize = pageSize
		}
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 获取配置列表
	response, err := service.GetUserCardFeeConfigs(query)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "GET_USER_CARD_FEE_CONFIGS_FAILED", err)
		return
	}

	ucommon.Success(ctx, response)
}

// DeleteUserCardFeeConfig 删除用户卡费配置
func (c *UserCardFeeConfigController) DeleteUserCardFeeConfig(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_ID", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 删除配置
	if err := service.DeleteUserCardFeeConfig(id); err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "DELETE_USER_CARD_FEE_CONFIG_FAILED", err)
		return
	}

	ucommon.Success(ctx, gin.H{"message": "删除成功"})
}

// GetUserCardFeeConfigsByUser 获取指定用户的所有卡费配置
func (c *UserCardFeeConfigController) GetUserCardFeeConfigsByUser(ctx *gin.Context) {
	userIDStr := ctx.Param("user_id")
	userID, err := strconv.ParseInt(userIDStr, 10, 64)
	if err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_USER_ID", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 获取用户配置
	configs, err := service.GetUserCardFeeConfigsByUser(userID)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "GET_USER_CARD_FEE_CONFIGS_FAILED", err)
		return
	}

	ucommon.Success(ctx, configs)
}

// GetUserActiveCardFeeConfigs 获取用户激活的卡费配置
func (c *UserCardFeeConfigController) GetUserActiveCardFeeConfigs(ctx *gin.Context) {
	userIDStr := ctx.Param("user_id")
	userID, err := strconv.ParseInt(userIDStr, 10, 64)
	if err != nil {
		ucommon.Error(ctx, http.StatusBadRequest, "INVALID_USER_ID", err)
		return
	}

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 获取用户激活的配置
	configs, err := service.GetUserActiveCardFeeConfigs(userID)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "GET_USER_ACTIVE_CARD_FEE_CONFIGS_FAILED", err)
		return
	}

	ucommon.Success(ctx, configs)
}

// GetCardFeeConfigs 获取卡费配置列表
func (c *UserCardFeeConfigController) GetCardFeeConfigs(ctx *gin.Context) {
	// 获取查询参数
	cardType := ctx.Query("card_type")
	cardFormFactor := ctx.Query("card_form_factor")
	currency := ctx.Query("currency")
	status := ctx.Query("status")

	page, _ := strconv.Atoi(ctx.DefaultQuery("pageIndex", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))

	// 创建服务实例
	service := service.NewUserCardFeeConfigService()

	// 获取卡费配置列表
	response, err := service.GetCardFeeConfigs(cardType, cardFormFactor, currency, status, page, pageSize)
	if err != nil {
		ucommon.Error(ctx, http.StatusInternalServerError, "GET_CARD_FEE_CONFIGS_FAILED", err)
		return
	}

	ucommon.Success(ctx, response)
}
